﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using Power.View;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Power.ViewModel
{
    public class DataCenterViewModel:ViewModelBase
    {
        private DataCenter dataCenter;
        public DataCenterViewModel(DataCenter dataCenter)
        {
            this.dataCenter = dataCenter;
            UserContent = GetSelectedUserControl(0);
            TabSwitch = new RelayCommand<object>(OnTabSwitch);
        }

        /// <summary>
        /// 页面切换
        /// </summary>
        /// <param name="sender"></param>
        public void OnTabSwitch(object sender)
        {
            string imgSrc = "/Resources/images/";
            Border curBorder = (Border)((Button)sender).Parent;
            
            List<Border> borderList = Common.Common.GetChildObjects<Border>((StackPanel)curBorder.Parent, 0);
            //移除所有同级元素选中效果
            int i = 1;
            borderList.ForEach((item) =>
            {
                //删除所有border背景色 
                item.Background = null;
                //下级节点样式还原
                
                //查找图片
                List<Image> imageList = Common.Common.GetChildObjects<Image>(item);
                imageList[0].Source = new BitmapImage(new Uri($"{imgSrc}dcl{i}.png", UriKind.Relative)); 
                imageList[1].Source = new BitmapImage(new Uri($"{imgSrc}dcl9.png", UriKind.Relative)); 

                //查找label
                List<Label> labelList = Common.Common.GetChildObjects<Label>(item);
                labelList[0].Foreground = new SolidColorBrush(Colors.Black);

                i++;
            });

            //设置当前元素的选中效果
            curBorder.Background = new SolidColorBrush(Color.FromRgb(0, 135, 211));
            //查找图片
            List<Image> curImageList = Common.Common.GetChildObjects<Image>(curBorder);
            //获取当前Border的Index
            int index = borderList.IndexOf(curBorder);
            curImageList[0].Source = new BitmapImage(new Uri($"{imgSrc}dcl{index + 1}{index + 1}.png", UriKind.Relative));
            curImageList[1].Source = new BitmapImage(new Uri($"{imgSrc}dcl99.png", UriKind.Relative));

            //查找label
            List<Label> curLabelList2 = Common.Common.GetChildObjects<Label>(curBorder);
            curLabelList2[0].Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
            if(index == 0)
            {
                curBorder.CornerRadius = new System.Windows.CornerRadius(15, 15, 0, 0);
            }
            //if (index == 6)
            //{
            //    var dlg = new SaveFileDialog()
            //    {
            //        Title = "另存为",
            //        DefaultExt = "txt",
            //        Filter = "Text files (*.txt)|*.txt|All files|*.*",
            //    };
            //    if (dlg.ShowDialog() == true)
            //    {
            //        File.WriteAllText(dlg.FileName, "test");
            //    }
            //}
            //else { 
                UserContent = GetSelectedUserControl(index);
            //}
        }


        #region Command
        public RelayCommand<object> TabSwitch { get; private set; }
        #endregion

        private UserControl GetSelectedUserControl(int index)
        {
            UserControl userControl = null;
            switch (index)
            {
                case 0:
                    userControl = new SiteManagement();
                    break;
                case 1:
                    userControl = new SectionManagement();
                    break;
                case 2:
                    userControl = new DataImport();
                    break;
                case 3:
                    userControl = new TestRecord();
                    break;
                case 4:
                    userControl = new EvaluationRecord();
                    break;
                case 5:
                    userControl = new DataAnalysis();
                    break;
                case 6:
                    userControl = new ReportGeneration();
                    break;
            }
            return userControl;
        }


        //内容呈现器绑定到UserContent
        private UserControl _content;
        public UserControl UserContent
        {
            get => _content;
            set
            {
                _content = value;
                RaisePropertyChanged("UserContent");
            }
        }
    }
}
